using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;

namespace Managers
{
    public class UserLoginManager
    {
        public event EventHandler OnUserLoggedIn;
        public event EventHandler OnUserLoggedOut;

        public static UserLoginManager Instance
        {
            get
            {
                
                if (HttpContext.Current.Session["UserManager"] == null)
                {
                    HttpContext.Current.Session["UserManager"] = new UserLoginManager();
                }

                return HttpContext.Current.Session["UserManager"] as UserLoginManager;
            }
        }

        public static User CurrentUser
        {
            get
            {
                // attempting to retrive user from Session
                User myUser = (User)HttpContext.Current.Session["currentUser"];
                if (myUser == null)
                {
                    // if the user isn't in the session, then attempt to retrive the user
                    // from cookies

                    myUser = Users.GetByUserIdAndPassword(UserIdCookie, UserPasswordCookie);
                    if (myUser != null)
                    {
                        // storing user in session
                        CurrentUser = myUser;
                    }
                }

                return myUser;
            }
            set
            {
                HttpContext.Current.Session["currentUser"] = value;
                if (value != null)
                {
                    UserIdCookie = value.ID;
                    UserPasswordCookie = value.Password;

                }
                else
                {
                    UserIdCookie = 0;
                    UserPasswordCookie = "";
                    HttpContext.Current.Session.Abandon();
                }
            }
        }

        public static Int64 UserIdCookie
        {
            get
            {
                return Convert.ToInt64(CookieManager.TryGetCookie("userID", "0"));
            }
            set
            {
                CookieManager.SetCookie("userID", value.ToString(), DateTime.Now.AddYears(1));
            }
        }

        public static string UserPasswordCookie
        {
            get
            {
                return CookieManager.TryGetCookie("password", "");
            }
            set
            {
                CookieManager.SetCookie("password", value, DateTime.Now.AddYears(1));
            }
        }

        public static bool BannedCookie
        {
            get
            {
                return CookieManager.TryGetCookie("fs", "false").Equals("true");
            }
            set
            {
                if (value == true)
                {
                    CookieManager.SetCookie("fs", "true", DateTime.Now.AddYears(1));
                }
                else
                {
                    CookieManager.SetCookie("fs", "false", DateTime.Now.AddYears(1));
                }
            }
        }

        public static User Login(string username, string cleartextPassword)
        {
            User myUser = DAL.Users.GetByUsernameAndPassword(username, Helpers.CryptoHelper.SHA512(cleartextPassword));
            if (myUser != null)
            {
                CurrentUser = myUser;
                if (Instance.OnUserLoggedIn != null)
                {
                    Instance.OnUserLoggedIn(Instance, new EventArgs());
                }
            }
            else
            {
                Logout();
            }

            return myUser;
        }

        public static void Logout()
        {
            CurrentUser = null;
            if (Instance.OnUserLoggedOut != null)
            {
                Instance.OnUserLoggedOut(Instance, new EventArgs());
            }
        }
    }
}